package com.brayan.webapp.controller.general.converter;

import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

import com.brayan.webapp.controller.PubCategoryController;
import com.brayan.webapp.model.PubCategory;
import com.brayan.webapp.repository.PubCategoryRepository;

@ManagedBean(name="pubCategoryConverterController")
@FacesConverter(value = "pubCategoryConverter")
public class PubCategoryConverter implements Converter {

	@Inject
	private PubCategoryRepository pubCategoryRepository;

	public PubCategoryConverter() {
	}

	// from page to backing bean
	@Override
	public Object getAsObject(FacesContext ctx, UIComponent component,
			String value) {

		PubCategoryController pubCategoryController = ctx.getApplication()
				.evaluateExpressionGet(ctx, "#{pubCategoryController}",
						PubCategoryController.class);
//		
//		PubCategory pubCat = pubCategoryController.getCategoryById(new Long(value));
		PubCategory pubCat = pubCategoryRepository.getPubCategoryById(new Long(
				value));
		return pubCat;
	}

	// from backing bean to page
	@Override
	public String getAsString(FacesContext fc, UIComponent uic, Object o) {
		PubCategory pubCat = ((PubCategory) o);
		return pubCat.getId().toString();
	}
	

}
